package com.example.supplychainbackend.controller;

import com.example.supplychainbackend.controller.dto.DataResponse;
import com.example.supplychainbackend.controller.dto.blockchain.UploadInfo;
import com.example.supplychainbackend.controller.dto.production.ProductionCertDto;
import com.example.supplychainbackend.controller.dto.production.ProductionProcessDto;
import com.example.supplychainbackend.controller.dto.production.ProductionSchedulingDto;
import com.example.supplychainbackend.controller.dto.production.ProductionWorkorderDto;
import com.example.supplychainbackend.controller.dto.user.UserSigDto;
import com.example.supplychainbackend.service.ProductionService;
import com.example.supplychainbackend.tool.JwtUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/production")
@AllArgsConstructor
public class ProductionController {
    private ProductionService productionService;

    @PostMapping("/cert/{id}/chain")
    public DataResponse<UploadInfo> uploadProductionCert(@PathVariable Long id){
        UploadInfo res = productionService.uploadProductionCert(id);
        return DataResponse.success(res);
    }

    @PostMapping("/cert")
    public DataResponse<Long> createProductionCert(@RequestHeader("Authorization") String authHeader,
                                                   @RequestBody ProductionCertDto certDto){
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        certDto.setUserName(userSigDto.getUserName());
        certDto.setUserId(userSigDto.getUserId());
        certDto.setEnterpriseName(userSigDto.getEnterpriseName());
        certDto.setEnterpriseId(userSigDto.getEnterpriseId());
        Long res = productionService.createProductionCert(certDto);
        return DataResponse.success(res);
    }
    @PutMapping("/cert/{id}")
    public DataResponse<ProductionCertDto> updateProductionCert(@RequestHeader("Authorization") String authHeader,
                                                                @PathVariable Long id,@RequestBody ProductionCertDto certDto) throws Exception {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        certDto.setUserName(userSigDto.getUserName());
        certDto.setUserId(userSigDto.getUserId());
        certDto.setEnterpriseName(userSigDto.getEnterpriseName());
        certDto.setEnterpriseId(userSigDto.getEnterpriseId());
        ProductionCertDto res = productionService.updateProductionCert(id,certDto);
        return DataResponse.success(res);
    }
    @DeleteMapping("/cert/{id}")
    public DataResponse<Long> deleteProductionCert(@PathVariable Long id){
        Long res = productionService.deleteProductionCert(id);
        return DataResponse.success(res);
    }

    @GetMapping("/cert/list")
    public DataResponse<List<ProductionCertDto>> getListProductionCert(@RequestHeader("Authorization") String authHeader){
        List<ProductionCertDto> res = productionService.getListProductionCert();
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        String eid = userSigDto.getEnterpriseId();
        if (eid != null) {
            res = productionService.getListProductionCertByEid(eid);
        }
        return DataResponse.success(res);
    }

    @GetMapping("/cert/{id}")
    public DataResponse<ProductionCertDto> getByIdProductionCert(@PathVariable Long id) throws Exception {
        ProductionCertDto res = productionService.getByIdProductionCert(id);
        return DataResponse.success(res);
    }
    @PostMapping("/workorder/{id}/chain")
    public DataResponse<UploadInfo> uploadProductionWorkorder(@PathVariable String id){
        UploadInfo res = productionService.uploadProductionWorkorder(id);
        return DataResponse.success(res);
    }
    @PostMapping("/workorder")
    public DataResponse<String> createProductionWorkorder(@RequestHeader("Authorization") String authHeader,
                                                          @RequestBody ProductionWorkorderDto dto){
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        dto.setUserName(userSigDto.getUserName());
        dto.setUserId(userSigDto.getUserId());
        dto.setEnterpriseName(userSigDto.getEnterpriseName());
        dto.setEnterpriseId(userSigDto.getEnterpriseId());
        String res = productionService.createProductionWorkorder(dto);
        return DataResponse.success(res);
    }
    @PutMapping("/workorder/{id}")
    public DataResponse<ProductionWorkorderDto> updateProductionWorkorder(@RequestHeader("Authorization") String authHeader,
                                                                          @PathVariable String id, @RequestBody ProductionWorkorderDto dto){
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        dto.setUserName(userSigDto.getUserName());
        dto.setUserId(userSigDto.getUserId());
        dto.setEnterpriseName(userSigDto.getEnterpriseName());
        dto.setEnterpriseId(userSigDto.getEnterpriseId());
        ProductionWorkorderDto res = productionService.updateProductionWorkorder(id,dto);
        return DataResponse.success(res);
    }
    @DeleteMapping("/workorder/{id}")
    public DataResponse<Long> deleteProductionWorkorder(@PathVariable String id){
        Long res = productionService.deleteProductionWorkorder(id);
        return DataResponse.success(res);
    }

    @GetMapping("/workorder/list")
    public DataResponse<List<ProductionWorkorderDto>> getListProductionWorkorder(@RequestHeader("Authorization") String authHeader){
        List<ProductionWorkorderDto> res = productionService.getListProductionWorkorder();
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        String eid = userSigDto.getEnterpriseId();
        if (eid != null) {
            res = productionService.getListProductionWorkorderByEid(eid);
        }
        return DataResponse.success(res);
    }

    @GetMapping("/workorder/{id}")
    public DataResponse<ProductionWorkorderDto> getByIdProductionWorkorder(@PathVariable String id) throws Exception {
        ProductionWorkorderDto res = productionService.getByIdProductionWorkorder(id);
        return DataResponse.success(res);
    }

    @PostMapping("/scheduling/{id}/chain")
    public DataResponse<UploadInfo> uploadProductionScheduling(@PathVariable Long id){
        UploadInfo res = productionService.uploadProductionScheduling(id);
        return DataResponse.success(res);
    }
    @PostMapping("/scheduling")
    public DataResponse<Long> createProductionScheduling(@RequestHeader("Authorization") String authHeader,
                                                         @RequestBody ProductionSchedulingDto dto){
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        dto.setUserName(userSigDto.getUserName());
        dto.setUserId(userSigDto.getUserId());
        dto.setEnterpriseName(userSigDto.getEnterpriseName());
        dto.setEnterpriseId(userSigDto.getEnterpriseId());
        Long res = productionService.createProductionScheduling(dto);
        return DataResponse.success(res);
    }
    @PutMapping("/scheduling/{id}")
    public DataResponse<ProductionSchedulingDto> updateProductionScheduling(@RequestHeader("Authorization") String authHeader,
                                                                            @PathVariable Long id, @RequestBody ProductionSchedulingDto dto){
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        dto.setUserName(userSigDto.getUserName());
        dto.setUserId(userSigDto.getUserId());
        dto.setEnterpriseName(userSigDto.getEnterpriseName());
        dto.setEnterpriseId(userSigDto.getEnterpriseId());
        ProductionSchedulingDto res = productionService.updateProductionScheduling(id,dto);
        return DataResponse.success(res);
    }
    @DeleteMapping("/scheduling/{id}")
    public DataResponse<Long> deleteProductionScheduling(@PathVariable Long id){
        Long res = productionService.deleteProductionScheduling(id);
        return DataResponse.success(res);
    }

    @GetMapping("/scheduling/listbywo/{wo}")
    public DataResponse<List<ProductionSchedulingDto>> getListProductionSchedulingByWO(@PathVariable String wo){
        List<ProductionSchedulingDto> res = productionService.getListByWOProductionScheduling(wo);
        return DataResponse.success(res);
    }

    @GetMapping("/scheduling/list")
    public DataResponse<List<ProductionSchedulingDto>> getListProductionScheduling(@RequestHeader("Authorization") String authHeader){
        List<ProductionSchedulingDto> res = productionService.getListProductionScheduling();
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        String eid = userSigDto.getEnterpriseId();
        if (eid != null) {
            res = productionService.getListProductionSchedulingByEid(eid);
        }
        return DataResponse.success(res);
    }

    @GetMapping("/scheduling/{id}")
    public DataResponse<ProductionSchedulingDto> getByIdProductionScheduling(@PathVariable Long id) throws Exception {
        ProductionSchedulingDto res = productionService.getByIdProductionScheduling(id);
        return DataResponse.success(res);
    }

    @PostMapping("/process/{id}/chain")
    public DataResponse<UploadInfo> uploadProductionProcess(@PathVariable Long id){
        UploadInfo res = productionService.uploadProductionProcess(id);
        return DataResponse.success(res);
    }
    @PostMapping("/process")
    public DataResponse<Long> createProductionProcess(@RequestHeader("Authorization") String authHeader,
                                                      @RequestBody ProductionProcessDto dto){
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        dto.setUserName(userSigDto.getUserName());
        dto.setUserId(userSigDto.getUserId());
        dto.setEnterpriseName(userSigDto.getEnterpriseName());
        dto.setEnterpriseId(userSigDto.getEnterpriseId());
        Long res = productionService.createProductionProcess(dto);
        return DataResponse.success(res);
    }
    @PutMapping("/process/{id}")
    public DataResponse<ProductionProcessDto> updateProductionProcess(@RequestHeader("Authorization") String authHeader,
                                                                      @PathVariable Long id, @RequestBody ProductionProcessDto dto) throws Exception {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        dto.setUserName(userSigDto.getUserName());
        dto.setUserId(userSigDto.getUserId());
        dto.setEnterpriseName(userSigDto.getEnterpriseName());
        dto.setEnterpriseId(userSigDto.getEnterpriseId());
        ProductionProcessDto res = productionService.updateProductionProcess(id,dto);
        return DataResponse.success(res);
    }
    @DeleteMapping("/process/{id}")
    public DataResponse<Long> deleteProductionProcess(@PathVariable Long id){
        Long res = productionService.deleteProductionProcess(id);
        return DataResponse.success(res);
    }

    @GetMapping("/process/list")
    public DataResponse<List<ProductionProcessDto>> getListProductionProcess(@RequestHeader("Authorization") String authHeader){
        List<ProductionProcessDto> res = productionService.getListProductionProcess();
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        String eid = userSigDto.getEnterpriseId();
        if (eid != null) {
            res = productionService.getListProductionProcessByEid(eid);
        }
        return DataResponse.success(res);
    }

    @GetMapping("/process/{id}")
    public DataResponse<ProductionProcessDto> getByIdProductionProcess(@PathVariable Long id) throws Exception {
        ProductionProcessDto res = productionService.getByIdProductionProcess(id);
        return DataResponse.success(res);
    }

    @PutMapping("/workorder/finish/{id}")
    public DataResponse<ProductionWorkorderDto> finishProductionWorkorder(@PathVariable String id){
        ProductionWorkorderDto res = productionService.finishWorkOrder(id);
        return DataResponse.success(res);
    }

    @PutMapping("/scheduling/finish/{id}")
    public DataResponse<ProductionSchedulingDto> finishProductionScheduling(@PathVariable Long id){
        ProductionSchedulingDto res = productionService.finishScheduling(id);
        return DataResponse.success(res);
    }

    @ExceptionHandler(Exception.class)
    public DataResponse<Exception> enterpriseErrorHandler(Exception e) {
        return DataResponse.error(500,e);
    }
}
